package com.mvc;

import com.mvc.dao.DepartmentDao;
import com.mvc.entity.Department;
import com.mvc.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by zhudong on 2016/9/5.
 */
public class InitMain {

    private SessionFactory sessionFactory;

    public static void init(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-db.xml");
        SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Department d1 = new Department("工程部");
        Department d2 = new Department("财务部");
        Department d3 = new Department("生产部");
        User u1 = new User("dongge","123456","yuzhou346694246@126.com",15,d1);
        User u2 = new User("zeus","123456","zhudong@cnooc.com.cn",16,d2);
        User u3 = new User("hello","123456","442704997@qq.com",19,d3);
        User u4 = new User("baidu","123456","346694246@qq.com",19,d3);
        User u5 = new User("nicolas","123456","456694246@qq.com",19,d2);
        session.save(u1);
        session.save(u2);
        session.save(u3);
        session.save(u4);
        session.save(u5);
        tx.commit();
        session.close();
    }

    public static void testTransaction(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-db.xml");
        DepartmentDao departmentDao = (DepartmentDao)applicationContext.getBean("departmentDao");
        Department department = new Department();
        department.setName("设计部");
        departmentDao.save(department);
    }

    public static void main(String[] args) {
//        init();
//        testTransaction();
        User u = new User();
        System.out.printf(u.getClass().getName());
        System.out.println("----------");
        System.out.printf(u.getClass().getSimpleName());
        if(u.getClass().equals(User.class)){
            System.out.println("is equal");
        }
    }
}
